package com.jack.helloworld.fragment;


import android.app.Fragment;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.jack.helloworld.R;


/**
 * A simple {@link Fragment} subclass.
 */
public class MyFragment extends Fragment {

    TextView tvMyFragment;
    FragmentActivityListener fragmentActivityListener;


    public interface FragmentActivityListener {
        abstract void sendToActivityMessage(String message);
    }

    public MyFragment() {
        // Required empty public constructor
    }

    @Override
    public void onAttach(Context context) {
        fragmentActivityListener = (FragmentActivityListener) context;
        super.onAttach(context);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_my, container, false);
        tvMyFragment = (TextView) view.findViewById(R.id.tvMyFragment);
        Log.d("jack", "view:" + view  + " ,tvMyFragment:" + tvMyFragment);
        String text = getArguments().getString("etValue");
        tvMyFragment.setText(text);

        Toast.makeText(getActivity(), "fragment从activity获取到数据:" + text , Toast.LENGTH_SHORT).show();

        String sendMessage = "hello!";
        Toast.makeText(getActivity(), "fragment向activity发送数据:" + sendMessage, Toast.LENGTH_SHORT).show();
        if (fragmentActivityListener != null) {
            fragmentActivityListener.sendToActivityMessage(sendMessage);
        }

        return view;
    }

}
